package com.mairuide.menuinfo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mairuide._frame.base.BaseRepository;
import com.mairuide._frame.base.BaseServiceProxy;
import com.mairuide._frame.utils.HanyuPinyinHelper;
import com.mairuide.menurole.MenuRole;
import com.mairuide.menurole.MenuRoleService;
import com.mairuide.menuuser.MenuUser;
import com.mairuide.menuuser.MenuUserService;
import com.mairuide.platroleuser.PlatRoleUser;
import com.mairuide.platroleuser.PlatRoleUserService;

@Service
@Transactional(readOnly = true)
public class MenuInfoService extends BaseServiceProxy<MenuInfo> {

	@Resource
	private MenuRoleService menuRoleService;

	@Resource
	private PlatRoleUserService platRoleUserService;

	@Resource
	private MenuUserService menuUserService;

	@Resource
	private MenuInfoRepository menuInfoRepository;

	@Override
	protected BaseRepository<MenuInfo> getBaseRepository() {
		return menuInfoRepository;
	}

	/**
	 * 查询该用户的权限。如果查询出的数据只有一条，且menu_id为NULL，表示用户账号权限被清空。否则返回的是拥有的菜单。
	 * 
	 * @param userId
	 * @return
	 */
	public List<MenuInfo> findByUserId(Integer userId) {
		/* 拥有的菜单权限集合 */
		Set<Integer> menuIdSet = new LinkedHashSet<Integer>();

		/* 先查询账号权限数据 */
		List<MenuUser> menuUserList = menuUserService.findByUserId(userId);
		for (MenuUser menuUser : menuUserList) {
			menuIdSet.add(menuUser.getMenuId());
		}
		/* 如果没有查询出数据，则拥有的角色权限 */
		if (menuIdSet.size() == 0) {
			List<PlatRoleUser> roleUserList = platRoleUserService.findByUserId(userId);
			Set<Integer> roleIdSet = roleUserList.stream().map(c -> c.getRoleId()).collect(Collectors.toSet());
			List<MenuRole> menuRoleList = menuRoleService.findByRoleIdIn(roleIdSet);
			menuIdSet = menuRoleList.stream().map(c -> c.getMenuId()).collect(Collectors.toSet());
		}

		Iterator<Integer> menuIdIterator = menuIdSet.iterator();
		while (menuIdIterator.hasNext()) {
			Integer menuId = menuIdIterator.next();
			if (menuId == null) {
				menuIdIterator.remove();
			}
		}

		return menuInfoRepository.findByMenuIdInAndMenuStatusOrderByMenuLevelAscMenuIndexAsc(menuIdSet, 1);
	}
	
	/**
	 * 查询这些菜单ID集合的父级菜单集合
	 * @param menuIds 菜单ID集合
	 * @return
	 */
	public Set<Integer> findMenuParentIdByMenuIdIn(Collection<Integer> menuIds) {
		List<MenuInfo> MenuInfoList = menuInfoRepository.findByMenuIdInAndMenuStatusOrderByMenuLevelAscMenuIndexAsc(menuIds, 1);
		Set<Integer> menuParentIdSet = MenuInfoList.stream().map(r->r.getMenuParentId()).filter(r->r.intValue() != 0).collect(Collectors.toSet());
		return menuParentIdSet;
	}
	
	
	/**
	 * 模糊查询当前用户所属权限下的菜单信息
	 * @param name
	 * @param list
	 * @return
	 */
	public List<MenuInfo> getMenuInfoByMenuNameLike(String name, List<MenuInfo> list) {
		List<MenuInfo> results = new ArrayList<MenuInfo>();
		Pattern pattern = Pattern.compile(name);
		for (int i = 0; i < list.size(); i++) {
			//汉字
			String munuName = list.get(i).getMenuName();
			Matcher matcher = pattern.matcher(munuName);
			//拼音
			String pinyinMunuName = HanyuPinyinHelper.toHanyuPinyin(list.get(i).getMenuName());
			Matcher pinyinMatcher = pattern.matcher(pinyinMunuName);
			if (matcher.find()) {
				results.add(list.get(i));
			}else if(pinyinMatcher.find()) {
				results.add(list.get(i));
			}
		}
		if(StringUtils.isNotEmpty(name) && (name.equals("上传") 
				|| name.equals("上传文件") || name.equals("我要导入")
				|| name.equals("导入文件") || name.equals("导入账单")
				|| name.equals("我要上传"))) {
			
			MenuInfo m = new MenuInfo();
			m.setIsJumpManage(1);
			m.setMenuCode("billimport");
			m.setMenuId(51);
			m.setMenuIndex(1);
			m.setMenuLevel(3);
			m.setMenuName("账单导入");
			m.setMenuParentId(7);
			m.setMenuStatus(1);
			m.setMenuUrl("/bill/import");
			MenuInfo m2 = new MenuInfo();
			m2.setIsJumpManage(1);
			m2.setMenuCode("billimport");
			m2.setMenuId(51);
			m2.setMenuIndex(1);
			m2.setMenuLevel(3);
			m2.setMenuName("账单导入");
			m2.setMenuParentId(7);
			m2.setMenuStatus(1);
			m2.setMenuUrl("/bill/import");
			results.add(m);
			results.add(m2);
		}
		return results;
	}
	
}
